package schedule.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.faces.component.UICommand;
import javax.faces.component.UIForm;
import javax.faces.event.ActionEvent;

public class PleaseWork {

	private UIForm form;
	private UIForm tableForm;
	private UICommand addCommand;

	public inputHr[] hour;
	public inputMin[] minutes;
	public String[] daysOfWeek;
	public String currentStartHour = "800";
	public String currentStartMin = "0";
	public String currentEndHour = "900";
	public String currentEndMin = "0";
	public String currentDay = "";
	private inputTime addedTime;
	private List<inputHr> hrList;
	private List<inputTime> allTimes;
	private DataRequester requester;

	public PleaseWork() {
		hour = new inputHr[11];
		hour[0] = new inputHr("8am", 8);
		hour[1] = new inputHr("9am", 9);
		hour[2] = new inputHr("10am", 10);
		hour[3] = new inputHr("11am", 11);
		hour[4] = new inputHr("12pm", 12);
		hour[5] = new inputHr("1pm", 1);
		hour[6] = new inputHr("2pm", 2);
		hour[7] = new inputHr("3pm", 3);
		hour[8] = new inputHr("4pm", 4);
		hour[9] = new inputHr("5pm", 5);
		hour[10] = new inputHr("6pm", 6);
		minutes = new inputMin[4];
		minutes[0] = new inputMin("0", 00);
		minutes[1] = new inputMin("15", 15);
		minutes[2] = new inputMin("30", 30);
		minutes[3] = new inputMin("45", 45);
		allTimes = new ArrayList<inputTime>();
		requester = new DataRequester();
	}

	public String addNew() {
		form.setRendered(true);
		addCommand.setRendered(true);
		return null;
	}

	public String save() {
		addedTime = new inputTime(Integer.parseInt(currentStartHour),
				Integer.parseInt(currentStartMin),
				Integer.parseInt(currentEndHour),
				Integer.parseInt(currentEndMin));
		addedTime.setCurrentDay(calculateDayNum(currentDay));
		allTimes.add(addedTime);
		System.out.println("added startHour " + currentStartHour
				+ " and endHour " + currentEndHour + " on day " + currentDay);
		form.setRendered(false);
		addCommand.setRendered(true);
		return null;
	}

	public String cancel() {
		form.setRendered(false);
		addCommand.setRendered(true);
		return null;
	}

	public String delete() {
		allTimes.remove(addedTime);
		return null;
	}

	public String submit() {
		System.out.println("Submitting arrayList of size " + allTimes.size());
		requester.insertIntoAdvisor(allTimes);
		return null;
	}
	
	public String scheduleMeeting(){
		pullCompatibleTimes(1);
		return null;
	}

	public void pullCompatibleTimes(int advisorID){			
		ArrayList<inputTime> studentTimes = requester.getStudentTimes(advisorID);
		ArrayList<inputTime> advisorTimes = requester.getAdvisorTimes(advisorID);
		ArrayList<inputTime> compatibleTimes = generateListOfTimes(studentTimes, advisorTimes);				
		
	}
	private ArrayList<inputTime> generateListOfTimes(ArrayList<inputTime> studentTimes, ArrayList<inputTime> advisorTimes){		
		HashMap<String, Student> studentList = new HashMap<String, Student>();
		//goes through arrayList of inputTimes, creates a Student() for every unique bannerID, adds them to the list. 
		//if student already exists, update their available times
		for (inputTime a: studentTimes){
			String thisBannerID = a.bannerID;
			if (!studentList.containsKey(a.bannerID)){				
				Student newStudent = new Student(thisBannerID);
				newStudent.addTime(a);
				studentList.put(thisBannerID, newStudent);
			}else{
				Student existingStudent = studentList.get(thisBannerID);
				existingStudent.addTime(a);
				studentList.put(thisBannerID, existingStudent);
			}
		}
		//adds available times to the advisor object
		Advisor parentAdvisor = new Advisor(advisorTimes.get(0).advisorID);		
		for (inputTime b: advisorTimes){
			parentAdvisor.addTime(b);
		}
		//generate milTime and boolSchedules
		finalSchedule fs = new finalSchedule();
		parentAdvisor.createSchedules();
		milTimeSchedule advMilTime = parentAdvisor.milTime;
		// need to get minMeetTime from web page and insert here
		for (Student s : studentList.values()){
			s.createSchedules();
			s.compatMilTime = fs.generateResultMilSchedule(s.milTime, advMilTime);
			s.compatibleTimes = s.compatInputTime();
		}			
				
		ArrayList<inputTime> compatibleInputTimes = new ArrayList<inputTime>();
		return compatibleInputTimes;
	}
	//set milTime

	/*
	 * private sendOutEmails()
	 * 
	 */
	public void displayTable(ActionEvent event) {
		if (event.getComponent().getId().equalsIgnoreCase("hide")) {
			tableForm.setRendered(false);
		} else {
			tableForm.setRendered(true);
		}
	}

	public int calculateDayNum(String day) {
		int dayNum = 0;
		if (day.equals("M"))
			dayNum = 0;
		else if (day.equals("T"))
			dayNum = 1;
		else if (day.equals("W"))
			dayNum = 2;
		else if (day.equals("R"))
			dayNum = 3;
		else if (day.equals("F"))
			dayNum = 4;
		return dayNum;
	}

	// Crazy number of getters and setters
	public inputHr[] getHourList() {
		return hour;
	}

	public inputMin[] getMinList() {
		return minutes;
	}

	public String getCurrentEndHour() {
		return currentEndHour;
	}

	public void setCurrentEndHour(String currentEndHour) {
		this.currentEndHour = currentEndHour;
	}

	public String getCurrentEndMin() {
		return currentEndMin;
	}

	public void setCurrentEndMin(String currentEndMin) {
		this.currentEndMin = currentEndMin;
	}

	public String[] getDaysOfWeek() {

		daysOfWeek = new String[5];
		daysOfWeek[0] = "M";
		daysOfWeek[1] = "T";
		daysOfWeek[2] = "W";
		daysOfWeek[3] = "R";
		daysOfWeek[4] = "F";
		return daysOfWeek;
	}

	public void setCurrentDay(String currentDay) {
		this.currentDay = currentDay;
	}

	public String getCurrentDay() {
		return currentDay;
	}

	public List<inputHr> getHrList() {
		return hrList;
	}

	public UIForm getForm() {
		return form;
	}

	public void setForm(UIForm form) {
		this.form = form;
	}

	public UICommand getAddCommand() {
		return addCommand;
	}

	public void setAddCommand(UICommand addCommand) {
		this.addCommand = addCommand;
	}

	public UIForm getTableForm() {
		return tableForm;
	}

	public void setTableForm(UIForm tableForm) {
		this.tableForm = tableForm;
	}

	public String getCurrentStartHour() {
		return currentStartHour;
	}

	public void setCurrentStartHour(String currentHour) {
		this.currentStartHour = currentHour;
	}

	public String getCurrentStartMin() {
		return currentStartMin;
	}

	public void setCurrentStartMin(String currentMin) {
		this.currentStartMin = currentMin;
	}

	public List<inputTime> getAllTimes() {
		return allTimes;
	}

	public void setAllTimes(List<inputTime> allTimes) {
		this.allTimes = allTimes;
	}

	public inputTime getAddedTime() {
		return addedTime;
	}

	public void setAddedTime(inputTime addedTime) {
		this.addedTime = addedTime;
	}

}
